package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.zb.mapper.ViewGoodsMapper;
import com.zb.result.PageUtil;
import com.zb.service.ViewGoodsService;
import com.zb.view.ViewGoods;
import com.zb.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2024/1/13 0013
 **/
@Service
public class ViewGoodsServiceImpl implements ViewGoodsService {
    @Resource
    private ViewGoodsMapper viewGoodsMapper;

    // 根据[商品ID] & [商品规格ID]查询
    @Override
    public List<ViewGoods> getByGoodsIdsAndGoodsSpecIds(List<Long> goodsIds, List<Long> goodsSpecIds) {
        if (CollectionUtil.isEmpty(goodsIds) && CollectionUtil.isEmpty(goodsSpecIds)) {
            return new ArrayList<>();
        }
        return viewGoodsMapper.getByGoodsIdsAndGoodsSpecIds(goodsIds, goodsSpecIds);
    }

    // 获取出入库商品
    @Override
    public Map<String, Object> getInOrOutStoreGoods(Long custAccId, Integer type, OrderGoodsSelectVo goodsSelectVo) {
        PageUtil.setPageLimit(goodsSelectVo);
        List<ViewGoods>  viewGoodsList = viewGoodsMapper.getInOrOutStoreGoods(custAccId, type, goodsSelectVo);
        List<OrderGoodsListVo> voList = viewGoodsList.stream().map(goods -> {
            OrderGoodsListVo vo = new OrderGoodsListVo();
            BeanUtil.copyProperties(goods, vo);
            return vo;
        }).toList();
        return PageUtil.pageList(goodsSelectVo.getPage(), goodsSelectVo.getLimit(), viewGoodsMapper.getInOrOutStoreGoodsCount(custAccId, type, goodsSelectVo), voList);
    }

    // 获取商品导出Excel数据
    @Override
    public List<ViewGoods> getGoodsExportExcel(Long custAccId, Integer type, GoodsSelectVo selectVo) {
        return viewGoodsMapper.getGoodsExportExcel(custAccId, type, selectVo);
    }

    // 获取商品导出Excel数据总数量
    @Override
    public long getGoodsExportExcelCount(Long custAccId, Integer type, GoodsSelectVo selectVo) {
        return viewGoodsMapper.getGoodsExportExcelCount(custAccId, type, selectVo);
    }

    // 根据商家ID与商品类型获取商品信息
    @Override
    public List<ViewGoods> getByCustAccIdAndType(Long custAccId, Integer type) {
        return viewGoodsMapper.getByCustAccIdAndType(custAccId, type);
    }

    // 根据商家ID查询商品信息
    @Override
    public List<ViewGoods> getGoodsExportByCustAccId(Long custAccId) {
        return viewGoodsMapper.getGoodsExport(custAccId, null);
    }

    // 根据商家ID查询成品信息
    @Override
    public List<ViewGoods> getProductExportByCustAccId(Long custAccId) {
        return viewGoodsMapper.getGoodsExport(custAccId, 1);
    }

    // 根据[商品类型]&[商品ID]&[商品规格ID]
    @Override
    public ViewGoods getByGoodsTypeAndGoodsIdAndGoodsSpecId(Integer goodsType, Long goodsId, Long goodsSpecId) {
        return viewGoodsMapper.getByGoodsTypeAndGoodsIdAndGoodsSpecId(goodsType, goodsId, goodsSpecId);
    }

    // 获取工单报单产品
    @Override
    public Map<String, Object> getOrderProduct(Long custAccId, OrderGoodsSelectVo goodsSelectVo) {
        PageUtil.setPageLimit(goodsSelectVo);
        long count = viewGoodsMapper.getOrderProductListCount(custAccId, goodsSelectVo);
        if (count <= 0) {
            return PageUtil.pageList(goodsSelectVo.getPage(), goodsSelectVo.getLimit(), count, new ArrayList<>());
        }
        List<ViewGoods> viewGoodsList = viewGoodsMapper.getOrderProductList(custAccId, goodsSelectVo);
        return PageUtil.pageList(goodsSelectVo.getPage(), goodsSelectVo.getLimit(), count, viewGoodsList.stream().map(goods -> {
            OrderGoodsListVo vo = new OrderGoodsListVo();
            BeanUtil.copyProperties(goods, vo);
            return vo;
        }).toList());
    }

    // 获取Boom关联的商品列表
    @Override
    public Map<String, Object> getGoodsBoomList(Long custAccId, Integer goodsType, Long id, GoodsBoomSelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        long count = viewGoodsMapper.getGoodsBoomListCount(custAccId, goodsType, id, selectVo);
        if (count <= 0) {
            return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), count, new ArrayList<>());
        }
        List<ViewGoods> goodsBoomList = viewGoodsMapper.getGoodsBoomList(custAccId, goodsType, id, selectVo);
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), count, goodsBoomList.stream().map(goods -> {
            GoodsBoomListVo vo = new GoodsBoomListVo();
            BeanUtil.copyProperties(goods, vo);
            return vo;
        }).toList());
    }
}
